package com.ruoyi.web.service.consumer;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.ServiceHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysConsumer;
import com.ruoyi.system.domain.SysProduct;
import com.ruoyi.system.domain.SysWorkOrder;
import com.ruoyi.system.service.ISysConsumerService;
import com.ruoyi.system.service.ISysWorkOrderService;
import com.ruoyi.web.dto.client.WorkOrderSubmit;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
public class MpWorkOrderService {

    private final ServiceHelper helper;

    private final ISysWorkOrderService workOrderService;

    private final ISysConsumerService consumerService;

    /**
     * 提交工单
     * @param workOrderSubmit
     * @param userId
     * @return
     */
    public WorkOrderSubmit submit(WorkOrderSubmit workOrderSubmit, Long userId) {
        SysWorkOrder sysWorkOrder = helper.convertToModel(workOrderSubmit, SysWorkOrder.class);
        sysWorkOrder.setCreatedBy(userId);
        SysConsumer consumer = consumerService.getById(userId);
        sysWorkOrder.setCreatedName(consumer.getUserName())
                .setCreatedTime(LocalDateTime.now());
        if (!workOrderService.save(sysWorkOrder)) {
            throw new RuntimeException("提交工单失败");
        }
        return workOrderSubmit;
    }

    /**
     * 返回数据
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @param userId
     * @return
     */
    public TableDataInfo getList(String keyword, Integer pageNum, Integer pageSize, Long userId) {
        TableDataInfo result = new TableDataInfo();
        LambdaQueryWrapper<SysWorkOrder> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(keyword)){
            wrapper.and(x->x.like( SysWorkOrder::getProductName, keyword)
                    .or().like(SysWorkOrder::getAddress, keyword)
                    .or().like(SysWorkOrder::getCarNumber, keyword)
                    .or().like(SysWorkOrder::getClientName, keyword));
        }
        wrapper.eq(SysWorkOrder::getCreatedBy, userId);
        wrapper.orderByDesc(SysWorkOrder::getCreatedTime);
        Page<SysWorkOrder> page = workOrderService.page(new Page<>(pageNum, pageSize), wrapper);
        result.setRows(page.getRecords());
        result.setTotal(page.getTotal());
        return result;
    }

    /**
     * 获取详情
     * @param id
     * @param userId
     * @return
     */
    public SysWorkOrder detail(Integer id, Long userId) {
        SysWorkOrder workOrder = workOrderService.getById(id);
        if (workOrder==null){
            throw new RuntimeException("工单不存在");
        }
        return workOrder;
    }

}
